<?php
/*
 *      player.class.php
 *      
 *      Copyright 2009 Valery Tverdohleb <tvsdesign@gmail.com>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

class Player
{
	protected $userData = array();
	public function __construct($id)
	{
		$stmt = Engine::get()->db->prepare('SELECT `id`, `name`, `hp`, `mp`, `talk`, `talk_s`, `X`, `Y`, `Z`, `L`, `strength`, `e_strength`, `dexterity`, `e_dexterity`, `stamina`, `e_stamina`, `vitality`, `e_vitality`, `w_melee`, `w_e_melee`, `w_cold`, `w_e_cold`, `w_fire`, `w_e_fire`, `w_wave`, `w_e_wave`, `w_beam`, `w_e_beam`, `c_physics`, `c_e_physics`, `c_chemistry`, `c_e_chemistry`, `c_biology`, `c_e_biology`, `c_mechanics`, `c_e_mechanics`, `c_cybernetics`, `c_e_cybernetics`, `c_blacksmith`, `c_e_blacksmith`, `c_weaponry`, `c_e_weaponry`, `c_computers`, `c_e_computers`, `c_nanotech`, `c_e_nanotech`, `c_medicine`, `c_e_medicine` FROM `users` WHERE `id` = :id LIMIT 1;');
		$stmt->bindValue(':id', $id, PDO::PARAM_INT);
		$stmt->execute();
		if ($stmt->rowCount() != 1)
		{
			Engine::get()->access->logout();
			setcookie('ESID','0',1);
		}
		$this->userData = $stmt->fetch(PDO::FETCH_ASSOC);
		
		Engine::get()->loadMap($this->coords());
	}
	
	public function talking()
	{
		if ($this->userData['talk'] != 0)
		{
			return array('talk'=>$this->userData['talk'], 'talk_s'=>$this->userData['talk_s']);
		}
		else
		{
			return false;
		}
	}
	
	public function coords($C = '')
	{
		switch ($C)
		{
			case 'X':
			{
				return $this->userData['X'];
				break;
			}
			case 'Y':
			{
				return $this->userData['Y'];
				break;
			}
			case 'Z':
			{
				return $this->userData['Z'];
				break;
			}
			case 'L':
			{
				return $this->userData['L'];
				break;
			}
			default:
			{
				return array(
						'X' => $this->userData['X'],
						'Y' => $this->userData['Y'],
						'Z' => $this->userData['Z'],
						'L' => $this->userData['L']
					);
			}
		}
	}
	
	public function __destruct()
	{
		$stmt = Engine::get()->db->prepare('UPDATE `users` SET `X` = :X, `Y` = :Y, `Z` = :Z, `L` = :L, `talk` = :talk, `talk_s` = :talk_s, `last_activity` = DEFAULT WHERE `id` = :id');
		$stmt->bindValue(':X', $this->userData['X']);
		$stmt->bindValue(':Y', $this->userData['Y']);
		$stmt->bindValue(':Z', $this->userData['Z']);
		$stmt->bindValue(':L', $this->userData['L']);
		$stmt->bindValue(':talk', $this->userData['talk']);
		$stmt->bindValue(':talk_s', $this->userData['talk_s']);
		$stmt->bindValue(':id', $this->userData['id']);
		$stmt->execute();
	}
	
	public function iterate()
	{
		if (isset($_POST['m']))
		{
			switch ($_POST['m'])
			{
				case 'touch':
				{
					switch ($_POST['d'])
					{
						case 'player':
						{
							if (!is_numeric($_POST['i'])) throw new Application_Exception('Wrong input');
							if (Engine::get()->pmanager->isPlayerAtPos(Engine::get()->player->coords(), $_POST['i']))
							{
								return array('mode' => 'touch', 'who' => 'player', 'id' => $_POST['i']);
							}
							break;
						}
					}
					break;
				}
				case 'message':
				{
					switch ($_POST['d'])
					{
						case 'send':
						{
							if (!(Engine::get()->pmanager->isOnline($_POST['i']) ||	(Engine::get()->messaging->isPlayerInPhonebook($_POST['i']))))
							{
								break; //:TODO: Попытка послать сообщения игроку которого нет в тел. книге и нет в онлайне. Либо игрок ушел в оффлайн, либо попытка взлома. Дописать проверку.
							}
							if (mb_strlen($_POST['msgtext'], 'utf8') > 0)
							{
								Engine::get()->messaging->sendMessage($_POST['i'], $_POST['msgtext']);
							}
							break;
						}
						default:;
					}
					break;
				}
				case 'phonebook':
				{
					switch ($_POST['d'])
					{
						case 'add':
						{
							if (is_numeric($_POST['i']))
							{
								if (Engine::get()->pmanager->isPlayerAtPos(Engine::get()->player->coords(), $_POST['i']))
								{
									Engine::get()->messaging->addContact($_POST['i']);
								}
								
							}
							break;
						}
						case 'remove':
						{
							if (is_numeric($_POST['i']))
							{
								Engine::get()->messaging->removeContact($_POST['i']);
							}
							break;
						}
						default:;
					}
					break;
				}
				case 'attack':
				{
					break;
				}
				case 'talk':
				{
					switch ($_POST['d'])
					{
						case 'npc':
						{
							if (!$this->userData['talk'])
							{
								if (is_numeric($_POST['i']) && Engine::get()->npc->isHere($_POST['i']))
								{
									$this->talk($_POST['i']);
								}
								else
								{
									throw new Application_Exception('Wrong input');
								}
							}
							else
							{
								if (is_numeric($_POST['i']))
								{
									$this->talk($_POST['i']);
								}
								else
								{
									throw new Application_Exception('Wrong input');
								}
							}
							break;
						}
						default:;
					}
					break;
				}
				case 'move':
				{
					switch ($_POST['d'])
					{
						case 'n':
						{
							if (Engine::get()->map->isCellActive(0,1,0))
							{
								$this->move(0,1,0);
							}
							break;
						}
						case 's':
						{
							if (Engine::get()->map->isCellActive(0,-1,0))
							{
								$this->move(0,-1,0);
							}
							break;
						}
						case 'e':
						{
							if (Engine::get()->map->isCellActive(1,0,0))
							{
								$this->move(1,0,0);
							}
							break;
						}
						case 'w':
						{
							if (Engine::get()->map->isCellActive(-1,0,0))
							{
								$this->move(-1,0,0);
							}
							break;
						}
						case 'u':
						{
							if (Engine::get()->map->isCellActive(0,0,1))
							{
								$this->move(0,0,1);
							}
							break;
						}
						case 'd':
						{
							if (Engine::get()->map->isCellActive(0,0,-1))
							{
								$this->move(0,0,-1);
							}
							break;
						}
						case 'door':
						{
							if (!is_numeric($_POST['i'])) throw new Security_Exception('Wrong input passed.');
							$doors = Engine::get()->map->doors(Engine::get()->player->coords());
							foreach ($doors as $v)
							{
								if ($v['id'] == $_POST['i'])
								{
									$this->moveTo($v['X'],$v['Y'],$v['Z'],$v['L']);
									break;
								}
							}
							break;
						}
						default:;
					}
					break;
				}
				default:
				{
				}
			}
		}
		if ($this->userData['talk'] != '0')
		{
			$this->talk();
			return array('mode' => 'talk');
		}
		return array('mode' => 'idle');
	}

	/*
	 * Получение id активного игрока
	 */

	public function getId()
	{
		return $this->userData['id'];
	}

	public function move($dX, $dY, $dZ, $L = 0)
	{
		$this->userData['X'] += $dX;
		$this->userData['Y'] += $dY;
		$this->userData['Z'] += $dZ;
		$this->userData['L'] = $L;
		Engine::get()->loadMap($this->coords());
	}
	
	public function moveTo($X, $Y, $Z, $L)
	{
		$this->userData['X'] = $X;
		$this->userData['Y'] = $Y;
		$this->userData['Z'] = $Z;
		$this->userData['L'] = $L;
		Engine::get()->loadMap($this->coords());
	}
	
	public function talk($id=null)
	{
		if ($id != null)
		{
			if ($this->userData['talk'] == 0)
			{
				$this->userData['talk'] = $id;
				$this->userData['talk_s'] = 0;
			}
			else
			{
				$dialog = Engine::get()->npc->dialog($this->userData['talk'],$id);
				$this->userData['talk_s'] = $dialog['next'][0];
				if ($dialog['next'] == null)
				{
					$this->userData['talk'] = 0;
					$this->userData['talk_s'] = '0';
				}
			}
		}
	}
	
	
	
}

?>
